package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.dto.DepartmentDto;
import com.huike.dto.PageDepartmentDto;
import com.huike.entity.Department;
import com.huike.entity.SysUser;
import com.huike.exception.BaseException;
import com.huike.mapper.DepartmentMapper;
import com.huike.mapper.SysUserMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    //分页部门查询
    @Override
    public Result<List<Department>> list(PageDepartmentDto dto) {
        //参数校验
        if (dto == null) {
            throw new BaseException("参数异常");
        }

        Integer page = dto.getPage();
        Integer size = dto.getSize();
        Short status = dto.getStatus();
        String name = dto.getName();

        //查询
        LambdaQueryWrapper<Department> lqw = new LambdaQueryWrapper<>();

        if (status != null) {
            lqw.eq(Department::getStatus, status);
        }

        if (name != null) {
            lqw.like(Department::getDeptName, name);
        }

        lqw.eq(Department::getDelFlag,0);

        //根据排序字段排序
        lqw.orderByAsc(Department::getOrderNum);

//        Page<Department> departmentPage = departmentMapper.selectPage(new Page<>(page, size), lqw);
        List<Department> departments = departmentMapper.selectList(lqw);
//        PageResult<Department> departmentPageResult = new PageResult<>();
//        departmentPageResult.setRecords(departments);
        return Result.success(departments);
    }


    //查询所有部门
    @Override
    public Result<List<Department>> findAllDept() {
        List<Department> departments = departmentMapper.selectList(new LambdaQueryWrapper<Department>().eq(Department::getDelFlag,0));
        return Result.success(departments);
    }

    //新增部门
    @Override
    public Result add(DepartmentDto dto) {
        //参数校验
        if (dto == null) {
            throw new BaseException("参数异常");
        }

        Department department = new Department();
        //拷贝属性
        BeanUtils.copyProperties(dto, department);

        //插入
        departmentMapper.insert(department);
        return Result.success();

    }

    //根据id查询部门
    @Override
    public Result<Department> findById(Integer id) {
        //参数校验
        if (id == null) {
            throw new BaseException("参数异常");
        }

        Department department = departmentMapper.selectById(id);
        return Result.success(department);
    }

    //修改部门
    @Override
    public Result updateDepartment(DepartmentDto dto) {
        //参数校验
        if (dto == null||dto.getId()==null) {
            throw new BaseException("参数异常");
        }

        Department department = new Department();
        BeanUtils.copyProperties(dto, department);

        departmentMapper.updateById(department);
        return Result.success();
    }

    //根据id删除部门
    @Override
    public Result deleteById(Integer id) {
        //参数校验
        if (id == null) {
            throw new BaseException("参数异常");
        }

        Department department = departmentMapper.selectById(id);

        //判断部门是否还有员工 或者 是否还有子级部门
        LambdaQueryWrapper<Department> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Department::getParentDepartmentId, id);
        List<Department> departments = departmentMapper.selectList(lqw);

        //员工
        List<SysUser> sysUsers = sysUserMapper
                .selectList(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeptId, id));

        if (departments != null && departments.size() > 0) {
            return Result.error("删除失败！请先删除 " + department + " 下的子级组织或用户，然后重试！");
        }
        if (sysUsers != null && sysUsers.size() > 0) {
            return Result.error("删除失败！请先删除 " + department + " 下的子级组织或用户，然后重试！");
        }

        department.setDelFlag((short) 2);
        departmentMapper.updateById(department);
        return Result.success();
    }
}
